package org.siberia.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class GoogleLoginServlet extends HttpServlet
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
	{
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null) {
//            resp.setContentType("text/plain");
            resp.getWriter().println("<html><body>Hello, " + user.getNickname() + " " + req.getUserPrincipal().getName());
            resp.getWriter().println("</br><a href=\"" + userService.createLogoutURL(req.getRequestURI()) + "\">Sign out</a>");
            resp.getWriter().println("</html></body>");
        } else {
            resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
        }

	}
}
